﻿package com.plter.controls.asmax
{
	import com.plter.events.AScrollBarEvent;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Jaja as-max.cn
	 */
	public class ATextArea extends Sprite
	{
		/**
		 * 本例中所使用的文本
		 */
		private var txt:TextField = new TextField;
		private var vScrollBar:AVScrollBar = new AVScrollBar;
		private var hScrollBar:AHScrollBar = new AHScrollBar;
		private var hasBorder:Boolean = false;
		private var theBorderColor:uint = 0xDDDDDD;
		private var theTextFormat:TextFormat = new TextFormat;
		private var hasBackground:Boolean = false;
		private var theBackgroundColor:uint = 0xF3F3F3;
		private var bg:Shape = new Shape;
		
		public function ATextArea() :void
		{
			addChild(bg);
			
			addChild(txt);
			multiline = true;
			wordWrap = true;
			border = true;
			
			vScrollBar.x = txt.width - vScrollBar.width;
			vScrollBar.y = 1;
			addChild(vScrollBar);
			vScrollBar.height = txt.height;
			vScrollBar.visible = false;
			
			hScrollBar.y = txt.height - hScrollBar.height;
			hScrollBar.x = 1;
			addChild(hScrollBar);
			hScrollBar.width = txt.width;
			hScrollBar.visible = false;
			
			txt.type = TextFieldType.INPUT;
			txt.addEventListener(Event.CHANGE, txtChange);
			txt.addEventListener(MouseEvent.MOUSE_WHEEL, scrollTxt);
			vScrollBar.addEventListener(AScrollBarEvent.SCROLL, vScroll);
			hScrollBar.addEventListener(AScrollBarEvent.SCROLL, hScroll);
		}
		
		/**
		 * 只读属性，获得此文本域中所使用的文本
		 */
		public function get textField():TextField {
			return txt;
		}
		
		/**
		 * 获得文本内容
		 */
		public function get text():String {
			return txt.text;
		}
		/**
		 * 设置文本内容
		 */
		public function set text(value:String):void {
			txt.text = value;
			
			txt.setTextFormat(theTextFormat);
			txtChange();
		}
		
		/**
		 * 设置文本的html值
		 */
		public function set htmlText(value:String):void {
			txt.htmlText = value;
			txtChange();
		}
		/**
		 * 获得文本的html值
		 */
		public function get htmlText():String {
			return txt.htmlText;
		}
		
		/**
		 * 向文本中添加内容
		 * @param	value 将要添加的内容
		 */
		public function appendText(value:String):void {
			txt.appendText(value);
			
			txt.scrollV = txt.maxScrollV;
			scrollTxt();
			vScrollBar.position = 1;
		}
		
		/**
		 * 设置文本的可编辑状态
		 */
		public function set editable(value:Boolean):void {
			txt.type = value?TextFieldType.INPUT:TextFieldType.DYNAMIC;
		}
		/**
		 * 获得文本的可编辑状态
		 */
		public function get editable():Boolean {
			switch(txt.type) {
				case TextFieldType.DYNAMIC:
					return false;
				case TextFieldType.INPUT:
					return true;
				default:
					return false;
			}
		}
		
		/**
		 * 指示文本域是否为多行,默认为多行
		 * @param	value
		 */
		public function set multiline(value:Boolean):void {
			txt.multiline = value;
		}
		/**
		 * 指示文本域是否为多行
		 */
		public function get multiline():Boolean {
			return txt.multiline;
		}
		
		/**
		 * 指示文本是否自动换行
		 */
		public function set wordWrap(value:Boolean):void {
			txt.wordWrap = value;
		}
		/**
		 * 指示文本是否自动换行
		 */
		public function get wordWrap():Boolean {
			return txt.wordWrap;
		}
		
		/**
		 * 获得此文本域的宽度
		 */
		public override function get width():Number { return super.width; }
		/**
		 * 设置此文本域的宽度
		 */
		public override  function set width(value:Number):void 
		{
			if (vScrollBar.visible) {
				txt.width = value-vScrollBar.width;
			}else {
				txt.width = value;
			}
			
			vScrollBar.x = value - vScrollBar.width;
			hScrollBar.width = txt.width;
			
			if (hasBorder) {
				drawBG(value, this.height);
			}
		}
		
		/**
		 * 获得此文本域的高度
		 */
		public override function get height():Number { return super.height; }
		/**
		 * 设置此文本域的高度
		 */
		public override  function set height(value:Number):void 
		{
			if (hScrollBar.visible) {
				txt.height = value-hScrollBar.height;
			}else {
				txt.height = value;
			}
			
			hScrollBar.y = value - hScrollBar.height;
			vScrollBar.height = txt.height;
			
			if (hasBorder) {
				drawBG(this.width, value);
			}
		}
		
		/**
		 * 设置文本的大小
		 * @param	wid	宽度
		 * @param	hei	高度
		 */
		public function setSize(wid:Number, hei:Number):void {
			if (vScrollBar.visible) {
				txt.width = wid - vScrollBar.width;
			}else {
				txt.width = wid;
			}
			vScrollBar.x = wid - vScrollBar.width;
			
			if (hScrollBar.visible) {
				txt.height = hei - hScrollBar.height;
			}else {
				txt.height = hei;
			}
			hScrollBar.y = hei - hScrollBar.height;
			
			hScrollBar.width = txt.width;
			vScrollBar.height = txt.height;
			
			if (hasBorder) {
				drawBG(wid, hei);
			}
		}
		
		/**
		 * 指定边框是否可见
		 */
		public function set border(value:Boolean):void {
			hasBorder = value;
			if (hasBorder) {
				drawBG(this.width, this.height);
			}else {
				graphics.clear();
			}
		}
		/**
		 * 指定边框是否可见
		 */
		public function get border():Boolean {
			return hasBorder;
		}
		
		/**
		 * 设置边框颜色
		 */
		public function set borderColor(value:uint):void {
			theBorderColor = value;
			
			if(border){
				drawBG(this.width, this.height);
			}
		}
		/**
		 * 获得边框颜色
		 */
		public function get borderColor():uint {
			return theBorderColor;
		}
		
		/**
		 * 设置文本样式
		 */
		public function set textFormat(value:TextFormat):void {
			theTextFormat = value;
			
			txt.setTextFormat(theTextFormat);
		}
		/**
		 * 获得文本样式
		 */
		public function get textFormat():TextFormat {
			return theTextFormat;
		}
		
		/**
		 * 设置文本字体颜色
		 */
		public function set textColor(value:uint):void {
			txt.textColor = value;
		}
		/**
		 * 获得文本字体颜色
		 */
		public function get textColor():uint {
			return txt.textColor;
		}
		
		/**
		 * 设置输入内容的限制字符
		 */
		public function set restrict(value:String):void {
			txt.restrict = value;
		}
		/**
		 * 获得输入内容的限制字符
		 */
		public function get restrict():String {
			return txt.restrict;
		}
		
		/**
		 * 指示是否显示背景色
		 */
		public function set background(value:Boolean):void {
			hasBackground = value;
			
			drawBG(this.width, this.height);
		}
		/**
		 * 指示是否显示背景色
		 */
		public function get background():Boolean {
			return hasBackground;
		}
		
		/**
		 * 设置背景色
		 */
		public function set backgroundColor(value:uint):void {
			theBackgroundColor = value;
			
			if (hasBackground) {
				drawBG(this.width, this.height);
			}
		}
		/**
		 * 获得背景色
		 */
		public function get backgroundColor():uint {
			return theBackgroundColor;
		}
		
		/**
		 * 设置背景透明度
		 */
		public function set backgroundAlpha(value:Number):void {
			bg.alpha = value;
		}
		/**
		 * 获得背景透明度
		 */
		public function get backgroundAlpha():Number {
			return bg.alpha;
		}
		
		/**
		 * 设置文本域所允许的最大字符数
		 */
		public function set maxChars(value:uint):void {
			txt.maxChars = value;
		}
		/**
		 * 获得文本域所允许的最大字符数
		 */
		public function get maxChars():uint {
			return txt.maxChars;
		}
		
		/**
		 * 获得字符串的长度
		 */
		public function get length():int {
			return txt.length;
		}
		
		/**
		 * 获得垂直滚动位置
		 */
		public function get scrollV():int {
			return txt.scrollV;
		}
		/**
		 * 设置垂直滚动位置
		 */
		public function set scrollV(value:int):void {
			txt.scrollV = value;
			
			vScrollBar.position = txt.maxScrollV / txt.maxScrollV;
		}
		
		/**
		 * 设置水平滚动条的位置
		 */
		public function set scrollH(value:int):void {
			txt.scrollH = value;
		}
		/**
		 * 获得水平滚动条的位置
		 */
		public function get scrollH():int {
			return txt.scrollH;
		}
		
		/**
		 * 获得最大垂直滚动条的位置
		 */
		public function get maxScrollV():int {
			return txt.maxScrollV;
		}
		/**
		 * 获得最大水平滚动条的位置
		 */
		public function get maxScrollH():int {
			return txt.maxScrollH;
		}
		
		/**
		 * 指示文本域是否可选
		 */
		public function get selectable():Boolean {
			return txt.selectable;
		}
		/**
		 * 指示文本域是否可选
		 */
		public function set selectable(value:Boolean):void {
			txt.selectable = value;
		}
		
		/**
		 * 获得选中部分的起始位置
		 */
		public function get selectionBeginIndex():int {
			return txt.selectionBeginIndex;
		}
		
		/**
		 * 获得选中部分的终点位置
		 */
		public function get selectionEndIndex():int {
			return txt.selectionEndIndex;
		}
		
		/**
		 * 指示在复制粘贴时是否具有格式
		 */
		public function set useRichTextClipboard(value:Boolean):void {
			txt.useRichTextClipboard = value;
		}
		/**
		 * 指示在复制粘贴时是否具有格式
		 */
		public function get useRichTextClipboard():Boolean {
			return txt.useRichTextClipboard;
		}
		
		/**
		 * 替换文本字段
		 * @param	beginIndex	开始位置
		 * @param	endIndex	结束位置
		 * @param	newText		新字符
		 */
		public function replaceText(beginIndex:int, endIndex:int, newText:String):void {
			txt.replaceText(beginIndex, endIndex, newText);
		}
		
		/**
		 * 替换选中的文本字段
		 * @param	value	新字符串
		 */
		public function replaceSelectedText(value:String):void {
			txt.replaceSelectedText(value);
		}
		
		/**
		 * 设置选中的文本字段
		 * @param	beginIndex	开始位置
		 * @param	endIndex	结束位置
		 */
		public function setSelection(beginIndex:int, endIndex:int):void {
			txt.setSelection(beginIndex, endIndex);
		}
		
		/**
		 * 指示文本是否总是显示选中部分
		 */
		public function set alwaysShowSelection(value:Boolean):void {
			txt.alwaysShowSelection = value;
		}
		/**
		 * 指示文本是否总是显示选中部分
		 */
		public function get alwaysShowSelection():Boolean {
			return txt.alwaysShowSelection;
		}
		
		/**
		 * 文本字符变化
		 * @param	event
		 */
		private function txtChange(event:Event = null):void {
			if (txt.textHeight > txt.height) {
				if(!vScrollBar.visible){
					vScrollBar.visible = true;
					txt.width = vScrollBar.x;
					hScrollBar.width = txt.width;
				}
				
				vScrollBar.barScale = txt.height / txt.textHeight;
			}else {
				if(vScrollBar.visible){
					vScrollBar.visible = false;
					txt.width = vScrollBar.x + vScrollBar.width;
					hScrollBar.width = txt.width;
				}
			}
			
			if (txt.textWidth > txt.width) {
				if (!hScrollBar.visible) {
					hScrollBar.visible = true;
					txt.height = hScrollBar.y;
					vScrollBar.height = txt.height;
				}
				hScrollBar.barScale = txt.width / txt.textWidth;
			}else {
				if (hScrollBar.visible) {
					hScrollBar.visible = false;
					txt.height = hScrollBar.y + hScrollBar.height;
					vScrollBar.height = txt.height;
				}
			}
			
			scrollTxt();
		}
		
		private function scrollTxt(event:Event = null):void {
			vScrollBar.position = txt.scrollV / txt.maxScrollV;
		}
		
		private function vScroll(event:AScrollBarEvent):void {
			txt.scrollV = event.position * txt.maxScrollV;
		}
		
		private function hScroll(event:AScrollBarEvent):void {
			txt.scrollH = event.position * txt.maxScrollH;
		}
		
		/**
		 * 绘制边框
		 * @param	wid	边框的宽度
		 * @param	hei	边框的高度
		 */
		private function drawBG(wid:Number, hei:Number):void {
			bg.graphics.clear();
			if(hasBorder){
				bg.graphics.lineStyle(1, theBorderColor);
			}else {
				bg.graphics.lineStyle();
			}
			if (hasBackground) {
				bg.graphics.beginFill(theBackgroundColor);
			}else {
				bg.graphics.beginFill(theBackgroundColor, 0);
			}
			bg.graphics.drawRoundRect( 0, 0, wid - .5, hei, 1, 1);
			bg.graphics.endFill();
		}
		
	}
	
}